#include"threadPool.h"
void deleteChar(char str[]) {
    int strLength = strlen(str); // 计算字符串的长度
    int point = 0; // 字符串位置标号，用于保存当前有效字符位置
    for (int i = 0; i < strLength; i++) {
        if (i!=0&& str[i] == '/'&&str[i-1]=='/') {
            continue; // 若当前字符为要删除的字符，则直接跳过继续进行循环
        } else {
            str[point] = str[i]; // 当前字符为非删除字符，则往前覆盖
            point++; // 有效字符标号位置递增
        }
    }
    str[point] = '\0'; // 循环结束，最后一个字符为结束符
}
int clean_point(char arg[])//去除绝对路径中的 . 和 .. 与 重复的 /
{
    char ant[300]={0};
    ant[0]='/';

    char deal[50][50];
    bzero(deal,sizeof(deal));

    char * one_name=strtok(arg,"/");
    int i=0;
    printf("delete point init over\n");
    while(one_name)
    {
        printf("one_name is %s\n",one_name);
        if(strcmp(one_name,".")==0);
        else if(strcmp(one_name,"..")==0&&i>0)i--;
        else
        {
            strcpy(deal[i],one_name);
            i++;
        }
        one_name=strtok(NULL,"/ ");
    }
    printf("deal shuzu overi---------------------------------\n");
    if(i==0)
    {
        strcpy(arg,"/");
        printf("仅有根\n");
        return 0;
    }
    for(int j=0;j<i;j++)
    {
        strcat(ant,deal[j]);
        if(j==i-1)continue;
        strcat(ant,"/");
    }
    printf("将要在数据库中查找的path是%s\n",ant);
    memcpy(arg,ant,sizeof(ant));
    return 0;
}
int relative_path_to_abs_path(char * relative_path,numStack_t * stack,MYSQL * mysql,char * abs_path)
{//传入是相对路径，传出是绝对路径
    char ant[500]={0};
    func_pwd(ant,stack,mysql);//拼接上当前work路径
    strcat(ant,"/");
    strcat(ant,relative_path);//拼接上相对路径
    deleteChar(ant);
    strcpy(abs_path,ant);
    return 0;
}

int func_cd(char * path,numStack_t * stack,MYSQL * mysql,char * user_name)
{
    if(path[0]=='/')//是绝对路径
    {
        clean_point(path);//设计一个函数清理path中的. 和 ..
        char sql[1024]={0};
        sprintf(sql,"select * from file_excel where path = '%s' and is_deleted = 0 and user_name = '%s' and type = 'd' ;",path,user_name);
        int ret=mysql_query(mysql,sql);
        MYSQL_RES *aaa = mysql_store_result(mysql);//得到查询结果
        if(ret!=0)                                    
        {
            printf("Error: %s\n", mysql_error(mysql));
            printf("用户cd的目标不存在\n");
            mysql_free_result(aaa);
            return -1;            
        }
        //判断是否查询到的为空
        MYSQL_ROW row=mysql_fetch_row(aaa); 
        if(row==NULL)
        {
            mysql_free_result(aaa);
            printf("用户cd的目标不存在\n");
            return -1;
        }
        mysql_free_result(aaa);
        //用户cd的目标存在 将栈改变
        printf("出栈\n");
        for(int i=0;i<stack->stackSize-1;i++)popStack(stack);//栈底恒为用户根节点的id所以只弹出stackSize-1次
        char * one_name=strtok(path,"/");
        printf("入栈\n");
        while(one_name)
        {
            bzero(sql,sizeof(sql));
            sprintf(sql,"select * from file_excel where file_name = '%s' and user_name = '%s';",one_name,user_name);
            mysql_query(mysql,sql);
            MYSQL_RES *result = mysql_store_result(mysql);//得到查询结果
            MYSQL_ROW row=mysql_fetch_row(result);//取出查询结果的一行
            int now_id=atoi(row[0]);//每一行的第一列是id
            pushStack(stack,now_id);
            one_name=strtok(NULL,"/");
            mysql_free_result(result);
        }
        printf("栈调整完成\n");
    }
    else//是相对路径
    {
        char abs_path[500]={0};
        relative_path_to_abs_path(path,stack,mysql,abs_path);    
        printf("abs_path over\n");
        printf("abs_path is %s\n",abs_path);
        clean_point(abs_path);
        printf("clean point over\n");
        printf("----------------------------------------\n");
        printf("true path is %s\n",abs_path);
        char sql[1024]={0};
        sprintf(sql,"select * from file_excel where path = '%s' and is_deleted = 0 and user_name = '%s' and type = 'd' ;",abs_path,user_name);
        int ret=mysql_query(mysql,sql);
        MYSQL_RES *aaa = mysql_store_result(mysql);//得到查询结果
        if(ret!=0)                                    
        {
            printf("Error: %s\n", mysql_error(mysql));
            printf("用户cd的目标不存在\n");
            mysql_free_result(aaa);
            return -1;            
        }
        //判断查询结果是否为空
        MYSQL_ROW row=mysql_fetch_row(aaa); 
        if(row==NULL)
        {
            mysql_free_result(aaa);
            printf("用户cd的目标不存在\n");
            return -1;
        }
        mysql_free_result(aaa);
        //用户cd的目标存在 将栈改变
        printf("出站\n");
        for(int i=0;i<stack->stackSize-1;i++)popStack(stack);//栈底恒为用户根节点的id所以只弹出stackSize-1次
        char * one_name=strtok(abs_path,"/");
        printf("入栈\n");
        while(one_name)
        {
            bzero(sql,sizeof(sql));
            sprintf(sql,"select * from file_excel where file_name = '%s' and user_name = '%s' ;",one_name,user_name);
            mysql_query(mysql,sql);
            MYSQL_RES *result = mysql_store_result(mysql);//得到查询结果
            MYSQL_ROW row=mysql_fetch_row(result);//取出查询结果的一行
            int now_id=atoi(row[0]);//每一行的第一列是id
            pushStack(stack,now_id);
            one_name=strtok(NULL,"/");
            mysql_free_result(result);
        }
        printf("栈调整完成\n");
    }//relative path
    return 0;
}//func_cd finish

